<template>
  <div>
    <h1>局部自定义指令演示</h1>
    <el-table
      v-juan="{ fh: 200, that: this }"
      id="tableId"
      ref="myTable"
      border
      :data="tableData"
      style="width: 800px; margin: 15vh auto 0"
    >
      <el-table-column prop="id" label="编号" align="center"> </el-table-column>
      <el-table-column prop="name" label="姓名" align="center">
      </el-table-column>
      <el-table-column prop="phone" label="手机" align="center">
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import axios from "axios";
console.log("this", this);
let timer = null;
export default {
  directives: {
    juan: {
      bind: function (e, binding) {
        // console.log("元素-bind-e", e);
        // console.log("参数-binding-e", binding);
        let that = binding.value.that;
        setTimeout(() => {
          let elFather = e.querySelector(".el-table__body-wrapper");
          elFather.style['height'] = binding.value.fh+'px';
          elFather.style['overflow'] = "auto";
          elFather.style["overflow-x"] = "hidden";
          console.log('elFather check',elFather);
          let elSon = e.querySelector(".el-table__body");
          elFather.addEventListener("scroll", (e) => {
            // console.log("e", e);
            let fH = elFather.clientHeight;
            let sH = elSon.clientHeight;
            let top = elFather.scrollTop;
            if (sH - fH + 5 >= top) {
              that.pageOb.pageSize += 1;
              console.log("that.pageOb", that.pageOb);
              clearTimeout(timer);
              timer = setTimeout(() => {
                that.getData();
                console.log("调用接口啦");
              }, 500);
            }
          });
        }, 100);
      },
      // 指令的定义
      inserted: function (el) {
        // console.log('inserted-el',el);
      },
    },
  },
  data() {
    return {
      myTable: null,
      tableData: [],
      pageOb: {
        page: 1,
        pageSize: 8,
      },
    };
  },
  methods: {
    async getData() {
      let res = await axios({
        method: "get",
        url: `http://192.168.1.7:3000/api/getData?page=${this.pageOb.page}&pageSize=${this.pageOb.pageSize}`,
      });
      console.log("res:", res);
      this.tableData = res.data.data;
      console.log('this.tableData',this.tableData);
    },

  },
  async mounted() {
    await this.getData();
  },
};
</script>

<style scoped>

</style>
